<?php
namespace app\api\controller;
use think\Controller;
use think\Validate;
use think\Config;
use think\Db;

class Returnorder extends Controller{
    //归还订单列表
    public function orderList(){
       $user_id = input('post.user_id');
       if(!is_numeric($user_id)){
          return json_encode(array('code'=>401,'msg'=>'user_id类型错误'));
       }
       $order_status = input('post.status');
       if($order_status){
          if(!is_numeric($order_status)){
             return json_encode(array('code'=>401,'msg'=>'status类型有误'));
          }
          //order_status 为1000表示归还订单的全部状态;
          if($order_status != 1000){
             if($order_status == 5 OR $order_status == 6 OR $order_status == 7 OR $order_status == 8 OR $order_status == 9){
                $where = "(o.order_status = 5 OR o.order_status = 6 OR o.order_status = 7 OR o.order_status = 8 OR o.order_status = 9) AND type = 1";
             }else{
                $where = "o.order_status = {$order_status} AND type = 1";
             }
          }else{
             $where = "(o.order_status = 3 OR o.order_status = 10 OR o.order_status = 5 OR o.order_status = 6 OR o.order_status = 7 OR o.order_status = 8 OR o.order_status = 9) AND type = 1";
          }
       }else{
          return json_encode(['code'=>401,'msg'=>'status参数缺失']);
       }
       //order_type 1快递 2.自提
       $order_list = db('order')->alias('o')
                                ->field('o.order_id,o.order_sn,o.order_status,o.pay_price,o.order_type')
                                ->where($where)
                                ->where("o.user_id = {$user_id}")
                                ->order('o.lease_time desc')
                                ->select();
       foreach($order_list as $key=>$value){
          $order_list[$key]['goods'] = db('order_goods')->field('goods_id,spec_item,spec_item_key,goods_number,goods_price,deposit,rent')
                                                        ->where("order_id = {$value['order_id']}")
                                                        ->select();
          foreach($order_list[$key]['goods'] as $k=>$v){
             $goods_info = db('goods')->field('name,thumbnail_img')->where("goods_id = {$v['goods_id']}")->find();
             $order_list[$key]['goods'][$k]['goods_name'] = $goods_info['name'];
             if(!Validate::is($goods_info['thumbnail_img'],'url')){
                 $order_list[$key]['goods'][$k]['thumbnail_img'] = 'http://'.Config::get('oss.bucket').'.'.Config::get('oss.endpoint').$goods_info['thumbnail_img'];
             }
          }
          $order_list[$key]['goods_number_sum'] = db('order_goods')->where("order_id = {$value['order_id']}")->sum('goods_number');
       }
       if($order_list){
            return json_encode(array('code'=>200,'msg'=>'请求成功','order_list'=>$order_list));
       }else{
            return json_encode(array('code'=>201,'msg'=>'请求失败或暂时还没有订单'));
       } 
    }

    //待归还  归还中  已归还 状态下的订单数量统计
    public function orderSum(){
        $user_id = input('post.user_id');
        if(!is_numeric($user_id)){
           return json_encode(['code'=>401,'msg'=>'user_id类型错误']);
        }
        $tobereturn_order_sum = db('order')->where("user_id = {$user_id} AND order_status = 10 AND type = 1")->count();//待归还
        $returning_order_sum = db('order')->where("user_id = {$user_id} AND (order_status = 5 OR order_status = 6 OR order_status = 7 OR order_status = 8 OR order_status = 9) AND type = 1")->count();//归还中
        $returned_order_sum = db('order')->where("user_id = {$user_id} AND order_status = 3 AND type = 1")->count();//已归还
        return json_encode(['code'=>200,'msg'=>'请求成功','tobereturn_order_sum'=>$tobereturn_order_sum,'returning_order_sum'=>$returning_order_sum,'returned_order_sum'=>$returned_order_sum]);
    }

    //订单详情
    public function orderDetail(){
        $order_id = input('post.order_id');
        // $order_id = '205';
        if(!is_numeric($order_id)){
            return json_encode(['code'=>401,'msg'=>'order_id类型错误']);
        }
        $where = "order_id = {$order_id}";
        $order_info = db('order')->field('order_sn,user_id,order_status,store_id,consignee,name,phone,add_time,pay_time,shipping_time,confirm_time,pay_price,order_type,type,postage,deposit_price,lease_time,lease_end_time,lease_confirm_time,lease_number,overdue_number,reduction_number,overdue_price,abrasion_price,abrasion_explain,abrasion_img,return_type,business_numbers,business_shipping_id,business_payment_pay,refund_price,business_pay_time')
                                 ->where($where)
                                 ->find();
        if($order_info['type'] != 1){
            return json_encode(['code'=>401,'msg'=>'该订单不为共享订单']);
        }
        if($order_info['return_type'] == 1){
            $express_delivery_info = [];
            $express_delivery_info['numbers'] = $order_info['business_numbers'];
            unset($order_info['business_numbers']);
            $express_delivery_info['shipping_id'] = $order_info['business_shipping_id'];
            $express_delivery_info['shipping_name'] = db('shipping')->where("shipping_id = {$order_info['business_shipping_id']}")->value('name');
            unset($order_info['business_shipping_id']);
            $order_info['express_delivery_info'] = $express_delivery_info;
            $order_info['store_name'] = db('store')->where("store_id = {$order_info['store_id']}")->value('name');
        }
        //归还邮寄地址(商家信息)
        $order_info['store_info'] = db('store')->field('name as store_name,address,lat,lng,tel')->where("store_id = {$order_info['store_id']}")->find();
        //订单状态对应时间
        if($order_info['order_status'] == 10){
           $order_info['status_time'] = date("Y年m月d日",$order_info['lease_time']);
        }elseif(($order_info['order_status'] == 5) || ($order_info['order_status'] == 6) || ($order_info['order_status'] == 7) || ($order_info['order_status'] == 8) || ($order_info['order_status'] == 9)){
           if($order_info['return_type'] == 1){
              $order_info['status_time'] = date("Y年m月d日",$order_info['lease_end_time']);
           }elseif($order_info['return_type'] == 2 && $order_info['order_status'] != 5){
              $order_info['status_time'] = date("Y年m月d日",$order_info['lease_end_time']);
           }    
        }elseif($order_info['order_status'] == 3){
           $order_info['status_time'] = date("Y年m月d日",$order_info['confirm_time']);
        }
        
        //商品信息
        $order_info['goods'] = db('order_goods')->field('goods_id,spec_item,spec_item_key,goods_number,goods_price,deposit,rent,settlement')
                                                ->where($where)
                                                ->select();
        $order_info['goods_number_sum'] = db('order_goods')->where($where)->sum('goods_number');
        $deposit = [];
        $rent = [];
        foreach($order_info['goods'] as $k=>$vo){
            $goods_info = db('goods')->field('name,thumbnail_img,mode')->where("goods_id = {$vo['goods_id']}")->find();
            $order_info['goods'][$k]['goods_name'] = $goods_info['name'];
            $order_info['goods'][$k]['mode'] = $goods_info['mode'];
            if(!Validate::is($goods_info['thumbnail_img'],'url')){
                $order_info['goods'][$k]['thumbnail_img'] = 'http://'.Config::get('oss.bucket').'.'.Config::get('oss.endpoint').$goods_info['thumbnail_img'];
            }else{
                $order_info['goods'][$k]['thumbnail_img'] = $goods_info['thumbnail_img'];
            }
            $deposit[] = $vo['deposit']*$vo['goods_number'];
            $rent[] = $vo['rent']*$vo['goods_number'];
        }
        //共享信息
        $sharing_info = [];
        $deposit_sum = array_sum($deposit);
        $sharing_info['deposit_sum'] = $deposit_sum;
        if($deposit_sum != $order_info['deposit_price'] && $deposit_sum > $order_info['deposit_price']){
           $sharing_info['free_deposit'] = $deposit_sum - $order_info['deposit_price'];
        }else{
           $sharing_info['free_deposit'] = 0;
        }
        $sharing_info['postage'] = $order_info['postage'];
        unset($order_info['postage']);
        $sharing_info['pay_price'] = $order_info['pay_price'];
        unset($order_info['pay_price']);
        $sharing_info['lease_time'] = date("Y-m-d",$order_info['lease_time']);
        if($order_info['order_status'] != 10){
            $sharing_info['lease_end_time'] = date("Y-m-d",$order_info['lease_end_time']);
            if($order_info['lease_number'] != '' && $order_info['lease_number'] != 0){
                $sharing_info['lease_number'] = $order_info['lease_number'];
            }else{
                if($order_info['lease_end_time'] > $order_info['lease_time']){
                    $sharing_info['lease_number'] = ceil(($order_info['lease_end_time'] - $order_info['lease_time'])/86400);
                }else{
                    $sharing_info['lease_number'] = 1;
                } 
            }   
        }
        // dump($sharing_info);die;
        $order_info['sharing_info'] = $sharing_info;
        //逾期信息
        if($order_info['overdue_number'] > 0){
            $overdue_info = [];
            $overdue_info['actual_return_time'] = date('Y-m-d',$order_info['lease_end_time']);//实际归还时间
            $overdue_info['overdue_number'] = $order_info['overdue_number'];//逾期天数
            unset($order_info['overdue_number']);
            $overdue_info['overdue_price'] = $order_info['overdue_price'];//逾期费用
            unset($order_info['overdue_price']);
            $overdue_info['reduction_number'] = $order_info['reduction_number'];//减免天数
            unset($order_info['reduction_number']);
            $overdue_info['rent'] = array_sum($rent);//日租金
            $order_info['overdue_info'] = $overdue_info;
        }
        //磨损信息
        $wear_info = [];
        if($order_info['abrasion_price'] > 0){
            $wear_info['abrasion_explain'] = $order_info['abrasion_explain'];
            unset($order_info['abrasion_explain']);
            $wear_info['abrasion_price'] = $order_info['abrasion_price'];
            unset($order_info['abrasion_price']);
            if($order_info['abrasion_img']){
                $wear_info['abrasion_img'] = explode(',',$order_info['abrasion_img']);
                foreach($wear_info['abrasion_img'] as $key=>$value){
                    if(!Validate::is($value,'url')){
                         $wear_info['abrasion_img'][$key] = 'http://'.Config::get('oss.bucket').'.'.Config::get('oss.endpoint').$value;
                    }
                }
            }
            unset($order_info['abrasion_img']);
            $order_info['wear_info'] = $wear_info;
        }
        //归还须知
        $order_info['return_notice'] = db('config')->where("name = 'attention'")->value('value');
        //订单时间 下单 付款
        $order_info['add_time'] = date("Y-m-d H:i",$order_info['add_time']);//下单时间
        if($order_info['order_status'] != 0){
            $order_info['pay_time'] = date("Y-m-d H:i",$order_info['pay_time']);//支付时间
        }
        //商家发货时间
        if($order_info['order_type'] == 1){
            if($order_info['shipping_time'] != ''){
                $order_info['shipping_time'] = date("Y-m-d H:i",$order_info['shipping_time']);
            }    
        }
         //用户收货时间
         if($order_info['confirm_time'] != ''){
             $order_info['confirm_time'] = date("Y-m-d H:i",$order_info['confirm_time']);
         }
         //用户归还时间
         if($order_info['lease_end_time'] != ''){
             if($order_info['return_type'] == 1){
                $order_info['lease_end_time'] = date("Y-m-d H:i",$order_info['lease_end_time']); 
             }
         }
         //商家确认收货时间
         if($order_info['return_type'] == 1){
            if($order_info['lease_confirm_time'] == 0){$order_info['lease_confirm_time'] = '';}
            if($order_info['lease_confirm_time'] != ''){
                $order_info['lease_confirm_time'] = date("Y-m-d H:i",$order_info['lease_confirm_time']);
            }
         }elseif($order_info['return_type'] == 2){
            if($order_info['lease_end_time'] == 0){$order_info['lease_confirm_time'] = '';}
            if($order_info['lease_end_time'] != ''){
                $order_info['lease_confirm_time'] = date("Y-m-d H:i",$order_info['lease_end_time']); 
            }
         }
         //补缴费用时间
         if($order_info['business_pay_time'] == 0){$order_info['business_pay_time'] = '';}
         if($order_info['business_pay_time'] != ''){
             $order_info['business_pay_time'] = date("Y-m-d H:i",$order_info['business_pay_time']);
         }
         // dump($order_info);die;
        if($order_info){
            return json_encode(array('code'=>200,'msg'=>'请求成功','order_info'=>$order_info));
        }else{
            return json_encode(array('code'=>201,'msg'=>'请求失败'));
        }
    }

    //归还订单  支付  确认 (无论退款还是支付,只要实际支付押金小于总押金,就表明这笔订单包含了抵押押金,那么在完成这笔订单的同时,也需要退还这笔抵押押金)
    //退款 退的是实际支付押金减去可能存在的逾期费用及磨损费用后,仍有多余金额,所以需要退款;
    //支付 支付是指当支付押金减去可能存在的逾期费用及磨损费用后,金额不足,所以需要补交差额;
    public function confirmationPayment(){
        $order_id = input('post.order_id');
        if(!is_numeric($order_id)){
           return json_encode(['code'=>401,'msg'=>'order_id类型错误']);
        }
        $map = "order_id = {$order_id}";
        $order_info = db('order')->field('order_sn,user_id,order_status,pay_price,deposit_price,overdue_price,abrasion_price,business_payment_pay,refund_price')->where($map)->find();
        $transaction_id = db('order_pay')->where("order_id = {$order_id} AND type = 1")->value('pay_code');
        $openid = db('user')->where("user_id = {$order_info['user_id']}")->value('openid');
        if(($order_info['order_status'] == 8) || ($order_info['order_status'] == 9)){
            $goods_info = db('order_goods')->field('goods_id,spec_item,goods_number,deposit,code')->where($map)->select();
            $deposit = [];
            foreach($goods_info as $key => $value){
                $deposit[] = $goods_info[$key]['goods_number'] * $goods_info[$key]['deposit'];
            }
            $deposit_sum = array_sum($deposit);
            //如果总押金大于支付押金,此时需要返还抵押押金;
            if($deposit_sum > $order_info['deposit_price']){
                 $mortgage_deposit = $deposit_sum - $order_info['deposit_price'];//抵押押金
                 $string = "user_id = {$order_info['user_id']}";
                 $user_info = db('user')->field('quota,frozen_integral,available_integral')->where($string)->find();
                 //如果押金总额度 > 0,也就是此时存在京东信用对应的额度时,才会返还冻结押金及可用押金;即当用户解绑时,是不需要返还冻结押金及可用押金;
                 if($user_info['quota'] > 0){
                     $return_frozen_integral = $user_info['frozen_integral'] - $mortgage_deposit;//返还冻结押金(释放冻结押金)
                     // $return_available_integral = $user_info['available_integral'] + $mortgage_deposit;//返还可用押金
                     // 可用额度的计算  可用额度 = （总额度 - 冻结额度）> 0 ?总额度-冻结额度:0;
                     $return_available_integral = ($user_info['quota'] - $return_frozen_integral) > 0 ? ($user_info['quota'] - $return_frozen_integral) :0;
                     $returnData = array('frozen_integral'=>$return_frozen_integral,'available_integral'=>$return_available_integral);
                 }
            }
            if($order_info['order_status'] == 8){
                   Db::startTrans();
                   try{
                            //更改订单状态及订单完成时间
                            db('order')->where($map)->update(['order_status'=>3,'business_confirm_time'=>time(),'end_time'=>time()]);//确认订单
                            //如果总押金大于支付押金,此时需要返还抵押押金;
                            if($deposit_sum > $order_info['deposit_price']){
                                if(isset($returnData)){
                                   db('user')->where($string)->update($returnData); 
                                }   
                            }
                            //归还订单完成后,须将商品的库存补回去
                            foreach($goods_info as $k=>$vo){
                                $where = "goods_id = {$vo['goods_id']} AND price_key = {$vo['spec_item']}";
                                db('spec_goods_price')->where($where)->setInc('store',$vo['goods_number']);
                                $code = '';
                                $code = explode(',',$vo['code']);
                                foreach($code as $key=>$value){
                                   db('code')->where("code_id = {$value}")->update(['state'=>0,'update_time'=>'','sale_type'=>0]);
                                }
                            }
                            $logData = [];
                            $logData['order_id'] = $order_id;
                            $logData['u_id'] = $order_info['user_id'];
                            $logData['content'] = '用户确认异常信息。押金现付￥'.$order_info['deposit_price'].',';
                            if($order_info['overdue_price'] > 0){
                               $logData['content'] .= '逾期费用￥'.$order_info['overdue_price'].',';
                            }
                            if($order_info['abrasion_price'] > 0){
                               $logData['content'] .= '磨损费用￥'.$order_info['abrasion_price'].',';
                            }
                            if($order_info['refund_price'] > 0){
                               $logData['content'] .= '需退还押金￥'.$order_info['refund_price'].'。';
                            }
                            $logData['create_time'] = time();
                            db('order_log')->insert($logData);
                            //退款  需要用户确认的订单才需要退款;如果该订单既无逾期费用,也无磨损费用,此时在商家端直接退款且订单状态改为已完成;
                            if(($order_info['refund_price'] > 0) && ($order_info['overdue_price'] != '' || $order_info['abrasion_price'] != '')){
                                $systemLogData = [];
                                $systemLogData['order_id'] = $order_id;
                                $systemLogData['u_id'] = $order_info['user_id'];
                                $systemLogData['content'] = '系统自动退款。退款方式：微信；退款金额：'.$order_info['refund_price'].'。';
                                $systemLogData['create_time'] = time();
                                db('order_log')->insert($systemLogData);
                                // 实例支付接口
                                $pay = & load_wechat('Pay');
                                $out_refund_no = 'S'.generateOrderNum(); 
                                // 调用退款接口
                                $order_info['pay_price'] = 0.01;
                                $order_info['refund_price'] = 0.01;
                                $result = $pay->refund($order_info['order_sn'], $transaction_id, $out_refund_no, $order_info['pay_price']*100, $order_info['refund_price']*100, $op_user_id = null);
                            }
                            Db::commit();
                   }catch(\Exception $e){
                            Db::rollback();
                            return json_encode(['code' => 201,'msg' => '确认失败']);
                   }
                   return json_encode(['code' => 200,'msg' => '确认成功']);
            }elseif($order_info['order_status'] == 9){
                //支付
                if($order_info['business_payment_pay'] > 0){
                     //求商品名称
                     $goods_list = db('order_goods')->field('goods_id,goods_number')->where("order_id = {$order_id}")->select();
                     $goods_name = [];
                     foreach($goods_list as $g=>$v){
                        //求商品名
                        $goods_list_name = db('goods')->where("goods_id = {$v['goods_id']}")->value('name');
                        $goods_name[] = $goods_list_name.'*'.$v['goods_number'];
                     }
                     $goods_name = implode(',',$goods_name);
                     Db::startTrans();
                     try{ 
                            $order_business_sn = 'S'.generateOrderNum();
                            $data = array('order_business_sn'=>$order_business_sn);
                            $update_data = db('order')->where($map)->update($data);
                
                            // 实例支付接口
                            $pay = & load_wechat('Pay');
                            // 获取预支付ID
                            $notify_url = 'https://'.$_SERVER['HTTP_HOST'].'/api/Returnorder/notify';
                            $order_info['business_payment_pay'] = 0.01;
                            $goods_name = '共享珠宝（'.$goods_name.'，补缴费用）';
                            $prepayid = $pay->getPrepayId($openid,$goods_name, $order_business_sn, $order_info['business_payment_pay'] * 100, $notify_url, $trade_type = "JSAPI");

                            // 创建JSAPI签名参数包
                            $options = $pay->createMchPay($prepayid);
                            Db::commit();
                     } catch (\Exception $e){
                            Db::rollback();
                            return json_encode(['code' => 201,'msg' => '归还订单提交失败']);
                     }
                     return json_encode(['code' => 200,'msg' => '归还订单提交成功','pay' => $options,'order_id' => $order_id]);
                }
                
            }
        }else{
           return json_encode(['code'=>401,'msg'=>'订单状态不对']);
        }
        
    }

    //回调
    public function notify(){
        // 实例支付接口
        $pay = & load_wechat('Pay');

        // 获取支付通知
        $notifyInfo = $pay->getNotify();

        // 支付通知数据获取失败
        if($notifyInfo===FALSE){
            // 接口失败的处理
            echo $pay->errMsg;
        }else{
            //支付通知数据获取成功
             if ($notifyInfo['result_code'] == 'SUCCESS' && $notifyInfo['return_code'] == 'SUCCESS') {
                $order = db('order')->where("order_business_sn = '{$notifyInfo['out_trade_no']}'")->find();
                if($order['order_status'] != 9){
                    return FALSE;
                }
                Db::startTrans();
                try{
                    $data['order_status'] = 3;
                    $data['business_pay_time'] = time();
                    $data['end_time'] = time();
                    db('order') -> where("order_business_sn = '{$notifyInfo['out_trade_no']}'") -> update($data);
                    //归还订单完成后,须将商品的库存补回去
                    $goods_info = db('order_goods')->field('goods_id,spec_item,goods_number,deposit,code')->where("order_id = {$order['order_id']}")->select();
                    $deposit = [];
                    foreach($goods_info as $k=>$vo){
                        $where = "goods_id = {$vo['goods_id']} AND price_key = {$vo['spec_item']}";
                        db('spec_goods_price')->where($where)->setInc('store',$vo['goods_number']);
                        $code = '';
                        $code = explode(',',$vo['code']);
                        foreach($code as $key=>$value){
                           db('code')->where("code_id = {$value}")->update(['state'=>0,'update_time'=>'','sale_type'=>0]);
                        }
                        $deposit[] = $goods_info[$k]['goods_number'] * $goods_info[$k]['deposit'];
                    }
                    //如果总押金大于支付押金,此时需要返还抵押押金;
                    $deposit_sum = array_sum($deposit);
                    if($deposit_sum > $order['deposit_price']){
                         $mortgage_deposit = $deposit_sum - $order['deposit_price'];//抵押押金
                         $map = "user_id = {$order['user_id']}";
                         $user_info = db('user')->field('quota,frozen_integral,available_integral')->where($map)->find();
                         //如果押金总额度 > 0,也就是此时存在京东信用对应的额度时,才会返还冻结押金及可用押金;即当用户解绑时,是不需要返还冻结押金及可用押金;
                         if($user_info['quota'] > 0){
                             $return_frozen_integral = $user_info['frozen_integral'] - $mortgage_deposit;//返还冻结押金
                             // $return_available_integral = $user_info['available_integral'] + $mortgage_deposit;//返还可用押金
                             // 可用额度的计算  可用额度 = （总额度 - 冻结额度）> 0 ?总额度-冻结额度:0;
                             $return_available_integral = ($user_info['quota'] - $return_frozen_integral) > 0 ? ($user_info['quota'] - $return_frozen_integral) :0;
                             $returnData = array('frozen_integral'=>$return_frozen_integral,'available_integral'=>$return_available_integral);
                             db('user')->where($map)->update($returnData);
                         }
                    }
                    //订单微信支付返还信息
                    $addData['order_id'] = $order['order_id'];
                    $addData['type'] = 4;
                    $addData['pay_code'] = $notifyInfo['transaction_id'];
                    $addData['pay_price'] = $notifyInfo['total_fee']/100;
                    $addData['time'] = strtotime($notifyInfo['time_end']);
                    $addData['notify_info'] = json_encode($notifyInfo);
                    db('order_pay')->insert($addData);
                    //订单日志
                    $logData = [];
                    $logData['order_id'] = $order['order_id'];
                    $logData['u_id'] = $order['user_id'];
                    $logData['content'] = '用户确认异常信息。押金现付￥'.$order['deposit_price'].',';
                    if($order['overdue_price'] > 0){
                       $logData['content'] .= '逾期费用￥'.$order['overdue_price'].',';
                    }
                    if($order['abrasion_price'] > 0){
                       $logData['content'] .= '磨损费用￥'.$order['abrasion_price'].',';
                    }
                    if($order['business_payment_pay'] > 0){
                       $logData['content'] .= '已支付￥'.$order['business_payment_pay'].'。';
                    }
                    $logData['create_time'] = time();
                    db('order_log')->insert($logData);
                    Db::commit();
                 } catch (\Exception $e) {
                    Db::rollback();
                 }
                $pay->replyXml(['return_code' => 'SUCCESS', 'return_msg' => 'DEAL WITH SUCCESS']);
             }
        }
    }

    //归还宝贝
    public function returnBaby(){
         $return_type = input('post.type');
         // $return_type = 1;
         if(!is_numeric($return_type)){
            return json_encode(['code'=>401,'msg'=>'type类型错误']);
         }
         $order_id = input('post.order_id');
         // $order_id = 74;
         if(!is_numeric($order_id)){
            return json_encode(['code'=>401,'msg'=>'order_id类型错误']);
         }
         $order = db('order')->field('user_id,lease_time')->where("order_id = {$order_id}")->find();
         $logData = [];
         $logData['order_id'] = $order_id;
         $logData['u_id'] = $order['user_id'];
         $logData['create_time'] = time();
         //1.快递 2.自提(送货上门)
         if($return_type == 1){
             $shipping_id = input('post.shipping_id');
             $shipping_id = 1;
             if(!is_numeric($shipping_id)){
                return json_encode(['code'=>401,'msg'=>'shipping_id类型错误']);
             }
             $numbers = input('post.numbers');
             // $numbers = '123';
             if($numbers == '' || $numbers == 'null' || $numbers == 'undefined'){
                return json_encode(['code'=>401,'msg'=>'numbers参数缺失']);
             }
             $pictures = input('post.picture');
             //实际租赁天数;
             if(time() > $order['lease_time']){
                 $lease_number = ceil((time() - $order['lease_time'])/86400);
             }else{
                 $lease_number = 1;
             }
             $data = array('order_status'=>5,'lease_end_time' => time(),'lease_number'=>$lease_number,'return_type' => 1,'business_shipping_id' => $shipping_id,'business_numbers' => $numbers);
             if($pictures){$data['business_express'] = $pictures;}
             $shipping_name = db('shipping')->where("shipping_id = {$shipping_id}")->value('name');
             $logData['content'] = '用户发货归还。物流公司，'.$shipping_name.'；快递单号：'.$numbers;
         }elseif($return_type == 2){
             $return_time = strtotime(input('post.return_time'));
             if($return_time == '' || $return_time == 'null' || $return_time == 'undefined'){
                return json_encode(['code'=>401,'msg'=>'请选择归还日期']);
             }
             $data = array('order_status'=>5,'return_time' => $return_time,'return_type' => 2);
             $logData['content'] = '用户到店归还，归还日期为'.date('Y-m-d H:i',$return_time);
         }
         // dump($data);die;
         Db::startTrans();
         try{
            db('order')->where("order_id = {$order_id}")->update($data);
            db('order_log')->insert($logData);
            Db::commit();
            return json_encode(['code'=>200,'msg'=>'提交成功']);
         }catch(\Exception $e){
            Db::rollback();
            return json_encode(['code'=>201,'msg'=>'提交失败']);
         }

    }

    //快递公司列表
    public function courierCompanyList(){
        $company_list = db('shipping')->field('shipping_id,name')->select();
        if($company_list){
           return json_encode(['code'=>200,'msg'=>'请求成功','company_list'=>$company_list]);
        }else{
           return json_encode(['code'=>201,'msg'=>'暂无数据']);
        }
    }

    //上传图片
    public function submitPicture(){
        $picture = ApiUpload('picture','returnorder');

        if($picture){
          $info['status'] = 200;
          $info['msg'] = '上传成功';
          $info['picture'] = $picture;
        }else{
          $info['status'] = 201;
          $info['msg'] = '上传失败';
        }

        return json_encode($info);
    }
}